package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;

	/**
	 * 上传声音文件
	 * @param multipartFile
	 * @return
	 */
	@Operation(summary = "上传声音文件" )
	@PostMapping("/trackInfo/uploadTrack")
	public Result<Map<String,String>> uploadTrack(@RequestParam("file") MultipartFile multipartFile){
		return Result.ok(vodService.uploadTrack(multipartFile));
	}

	/**
	 * 保存声音信息
	 * @param trackInfoVo
	 * @return
	 */
	@TsLogin
	@Operation(summary = "保存声音信息")
	@PostMapping("/trackInfo/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
		//获取UserId
		Long userId = AuthContextHolder.getUserId();
		trackInfoService.saveTrackInfo (userId, trackInfoVo);
		return Result.ok();
	}

    /**
     * 分页查询声音信息
     * @param page
     * @param limit
     * @param trackInfoQuery
     * @return
     */
	@TsLogin
    @Operation(summary = "分页查询声音信息")
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable Long page,
                                                       @PathVariable Long limit,
                                                       @RequestBody TrackInfoQuery trackInfoQuery){
        Page<TrackListVo> pageInfo = new Page<TrackListVo>(page, limit);
		trackInfoQuery.setUserId(AuthContextHolder.getUserId());
        pageInfo = trackInfoService.findUserTrackPage(pageInfo, trackInfoQuery);
        return Result.ok(pageInfo);
    }

	/**
	 * 回显
	 * @param id
	 * @return
	 */
	@Operation(summary = "回显")
	@GetMapping("/trackInfo/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id){
		TrackInfo trackInfo = trackInfoService.getTrackInfo(id);
		return Result.ok(trackInfo);
	}


	/**
	 * 修改声音数据
	 * @param id
	 * @param trackInfoVo
	 * @return
	 */
	@Operation(summary = "修改声音数据")
	@PutMapping("/trackInfo/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable Long id,@RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(id,trackInfoVo);
		return Result.ok();
	}

	/**
	 * 删除声音数据
	 * @param id
	 * @return
	 */
	@Operation(summary = "删除声音数据")
	@DeleteMapping("/trackInfo/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}


	/**
	 * 查询专辑声音分页列表
	 * @param albumId
	 * @param page
	 * @param limit
	 * @return
	 */
	@TsLogin(required=false)
	@GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<Page<AlbumTrackListVo>> findAlbumTrackList(@PathVariable Long albumId,
															 @PathVariable Long page,
															 @PathVariable Long limit){
		Page<AlbumTrackListVo> pageInfo = new Page<>(page,limit);
		Long userId = AuthContextHolder.getUserId();
		Page<AlbumTrackListVo> pageResult =trackInfoService.findAlbumTrackList(pageInfo,albumId,userId);

		return Result.ok(pageResult);
	}

	/**
	 * 查询声音统计信息
	 * @param trackId
	 * @return
	 */
	@Operation(summary = "查询声音统计信息")
	@GetMapping("/trackInfo/getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId){
		TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
		return Result.ok(trackStatVo);
	}
}

